# Create your tests here.
import json

import requests


def test_get_request_info():
    resp = requests.request(
        method="post",
        url="http://127.0.0.1:8000/reqs/getrequestinfo?name=xiaomi&age=32&salary=14K&salary=25K",
        data={"a": 1, "b": 2},
        headers={"company": "Govee"},
    )

    print(resp.text)
    assert resp.status_code == 200


def test_info_method():
    resp = requests.request(method="post", url="http://127.0.0.1:8000/reqs/info/method")
    assert resp.status_code == 200
    assert resp.text == "POST"


def test_info_path():
    resp = requests.request(method="post", url="http://127.0.0.1:8000/reqs/info/path")
    assert resp.status_code == 200
    assert resp.text == "/reqs/info/path"


def test_info_get():
    resp = requests.request(
        method="post", url="http://127.0.0.1:8000/reqs/info/get?&a=1&b=2&c=3&c=4&c=5"
    )
    assert resp.status_code == 200
    assert resp.text == json.dumps({"a": "1", "b": "2", "c": "5"})


def test_info_post():
    resp = requests.request(
        method="post",
        url="http://127.0.0.1:8000/reqs/info/post",
        data={"a": 1, "b": 2, "c": 3},
    )
    assert resp.status_code == 200
    assert resp.text == json.dumps({"a": "1", "b": "2", "c": "3"})


def test_info_json():
    resp = requests.request(
        method="post",
        url="http://127.0.0.1:8000/reqs/info/json",
        json={"a": "陈锦峰", "b": 1, "c": 2},
    )

    assert resp.status_code == 200
    # assert resp.text == json.dumps({"a": "陈锦峰", "b": 1, "c": 2})
    assert resp.json() == {"a": "陈锦峰", "b": 1, "c": 2}


def test_info_cookie():
    resp = requests.request(
        method="post",
        url="http://127.0.0.1:8000/reqs/info/cookie",
        cookies={"name": "xiaomi", "age": "32"},
    )
    assert resp.status_code == 200
    assert resp.text == json.dumps({"name": "xiaomi", "age": "32"})


def test_info_session():
    sess = requests.Session()

    # 第一次请求
    resp = sess.request(method="post", url="http://127.0.0.1:8000/reqs/info/session")
    assert resp.status_code == 200
    assert resp.text == json.dumps({"num": 1})

    # 第二次请求
    resp = sess.request(method="post", url="http://127.0.0.1:8000/reqs/info/session")
    assert resp.status_code == 200
    assert resp.text == json.dumps({"num": 2})

    # 第三次请求
    resp = sess.request(method="post", url="http://127.0.0.1:8000/reqs/info/session")
    assert resp.status_code == 200
    assert resp.text == json.dumps({"num": 3})


def test_info_res():
    sess = requests.Session()
    resp = sess.request(
        method="post",
        url="http://127.0.0.1:8000/reqs/info/res",
        cookies={"name": "123"},
    )
    print(resp.json())
    print(resp.headers["Content-Type"])
    assert resp.status_code == 200
